#include "stdafx.h"
#include "ResUtil.h"

#include <gdiplus.h>
using namespace Gdiplus;

HCURSOR LoadCursorFromRes(UINT nResID) 
{ 
	HINSTANCE hInst = AfxGetResourceHandle();
	// Create temp file 
	TCHAR szTempPath[_MAX_PATH]; 
	TCHAR szTempFile[_MAX_PATH]; 

	if(!GetTempPath(_MAX_PATH, szTempPath)) return false; 
	if(!GetTempFileName(szTempPath, _T("ani"), 0, szTempFile)) return false; 

	//MessageBox(szTempFile); 
	HANDLE hFile = CreateFile(szTempFile, GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); 
	if(hFile == INVALID_HANDLE_VALUE) goto failure; 

	// Load resource 
	HRSRC hrsrc = FindResource(hInst, MAKEINTRESOURCE(nResID), _T("ANI")); 
	if(!hrsrc) goto failure; 

	HGLOBAL hrc = LoadResource(0, hrsrc); 
	if(hrc == 0) goto failure; 

	LPBYTE pRcData = (LPBYTE)LockResource(hrc); 
	if(pRcData == 0) goto failure; 

	DWORD dwRcSize = SizeofResource(0, hrsrc); 
	if(dwRcSize == 0) goto failure; 

	DWORD cb = 0; 
	if(!WriteFile(hFile, pRcData, dwRcSize, &cb, 0)) goto failure; 
	CloseHandle(hFile); 

	HCURSOR hcur = (HCURSOR)LoadImage(0, szTempFile, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE | LR_SHARED); 
	//DWORD dw = GetLastError(); 
	DeleteFile(szTempFile); 

	return hcur; 

failure: 
	if(hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); 
	DeleteFile(szTempFile); 
	return 0; 
}

BOOL LoadBitmapPngFromRes(UINT nResID, HBITMAP& hBitmap)
{
	BOOL bRc = FALSE;
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nResID), _T("PNG"));
	if ( hRsrc )
	{
		BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
		if ( lpRsrc )
		{
			LPVOID lpVoid = ::LockResource(lpRsrc);
			DWORD len = SizeofResource(hInst, hRsrc);

			HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
			BYTE* pmem = (BYTE*)GlobalLock(m_hMem);

			memcpy(pmem, lpRsrc, len);

			IStream* pstm = NULL;
			HRESULT ht = CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
			if ( ht == S_OK )
			{
				Bitmap* pBitmap = Bitmap::FromStream(pstm);
				if ( pBitmap )
				{
					Color clr(255, 255, 255, 255);
					pBitmap->GetHBITMAP(clr, &hBitmap);
					delete pBitmap;
					bRc = TRUE;
				}
			}

			pstm->Release();

			GlobalUnlock(m_hMem);
			GlobalFree(m_hMem);
		}

		FreeResource(lpRsrc);
	}

	return bRc;
}

BOOL LoadIconPngFromRes(UINT nResID, HICON& hIcon)
{
	BOOL bRc = FALSE;
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nResID), _T("PNG"));
	if ( hRsrc )
	{
		BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
		if ( lpRsrc )
		{
			LPVOID lpVoid = ::LockResource(lpRsrc);
			DWORD len = SizeofResource(hInst, hRsrc);

			HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
			BYTE* pmem = (BYTE*)GlobalLock(m_hMem);

			memcpy(pmem, lpRsrc, len);

			IStream* pstm = NULL;
			HRESULT ht = CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
			if ( ht == S_OK )
			{
				Bitmap* pBitmap = Bitmap::FromStream(pstm);
				if ( pBitmap )
				{
					pBitmap->GetHICON(&hIcon);
					delete pBitmap;
					bRc = TRUE;
				}
			}

			pstm->Release();

			GlobalUnlock(m_hMem);
			GlobalFree(m_hMem);
		}

		FreeResource(lpRsrc);
	}

	return bRc;
}

BOOL LoadPngFromRes(UINT nID, Image **pImg)
{
	BOOL bRc = FALSE;
	HINSTANCE hInst = AfxGetResourceHandle();
	HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nID), _T("PNG"));
	if ( hRsrc )
	{
		BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
		if ( lpRsrc )
		{
			LPVOID lpVoid = ::LockResource(lpRsrc);
			DWORD len = SizeofResource(hInst, hRsrc);

			HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
			BYTE* pmem = (BYTE*)GlobalLock(m_hMem);

			memcpy(pmem, lpRsrc, len);

			IStream* pstm = NULL;
			HRESULT ht = CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
			if ( ht == S_OK )
			{
				*pImg = Gdiplus::Image::FromStream(pstm);
				bRc = TRUE;
			}

			pstm->Release();

			GlobalUnlock(m_hMem);
			GlobalFree(m_hMem);
		}

		FreeResource(lpRsrc);
	}

	return bRc;
}

BOOL DrawPngResToDC(CDC* pDC, UINT nResID, int nX /*= 0*/, int nY /*= 0*/)
{
	Image *pImg = NULL;
	if (!LoadPngFromRes(nResID, &pImg))
	{
		return FALSE;
	}

	BOOL bRc = DrawImageToDC(pDC, pImg, nX, nY);
	
	if ( pImg )
	{
		delete pImg;
	}

	return bRc;
}

BOOL DrawImageToDC(CDC* pDC, Image *pImg, int nX /*= 0*/, int nY /*= 0*/)
{
	CRect rect;

	rect.left = 0;
	rect.top = 0;
	rect.right = pImg->GetWidth();
	rect.bottom = pImg->GetHeight();

	Graphics graphics(pDC->GetSafeHdc());

	//graphics.DrawImage(pBgImage, Rect(dstRt.left,dstRt.top,dstRt.Width(),dstRt.Height()),
	//					srcRect.left, srcRect.top, 
	//					srcRect.right, srcRect.bottom, 
	//					UnitPixel);

	
	graphics.DrawImage(pImg, nX, nY, rect.left, rect.top, rect.Width(), rect.Height(), UnitPixel);
	
	return TRUE;
}